<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="account">
          <el-input v-model="filters.account" placeholder="分销员名称/账号"></el-input>
        </el-form-item>
        <el-form-item prop="seller">
          <el-input v-model="filters.seller" placeholder="分销员等级"></el-input>
        </el-form-item>
        <el-form-item prop="time">
          <el-date-picker
            v-model="filters.time"
            type="daterange" value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="加入开始时间"
            end-placeholder="加入结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="filters.status" placeholder="账号状态">
            <el-option
              v-for="item in statusOption"
              :key="item.status"
              :label="item.label"
              :value="item.status"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item prop="startpeople" label="邀新人数">
            <el-input-number v-model="filters.startpeople" controls-position="right" :min="1"></el-input-number>
            <span>-</span>
            <el-input-number v-model="filters.endpeople" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item  label="有效邀新">
            <el-input-number v-model="filters.effectivepeople" controls-position="right" :min="1"></el-input-number>
            <span>-</span>
            <el-input-number v-model="filters.effectivepeoples" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="已入账邀新奖金">
            <el-input-number v-model="filters.entrybonus" controls-position="right" :min="1"></el-input-number>
            <span>-</span>
            <el-input-number v-model="filters.entrybonuss" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="分销订单">
            <el-input-number v-model="filters.sellerorder" controls-position="right" :min="1" :max="10"></el-input-number>
            <span>-</span>
            <el-input-number v-model="filters.sellerorders" controls-position="right" :min="1" :max="10"></el-input-number>
          </el-form-item>
          <el-form-item label="销售金额">
            <el-input-number v-model="filters.sales" controls-position="right" :min="1"></el-input-number>
            <span>-</span>
            <el-input-number v-model="filters.saless" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="已入账分销佣金">
            <el-input-number v-model="filters.entrymoney" controls-position="right" :min="1"></el-input-number>
            <span>-</span>
            <el-input-number v-model="filters.entrymoneys" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')"/>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="false"
      :customLabel='"禁用"'
      :showCustomOperation="true"
      :showDeleteOperation="false"
      :showBatchDelete="false"
      :showSelection="false"
      @findPage="findPage"
      @handleCustom="handleCustom"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="100px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        :label-position="labelPosition"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="分销员等级" prop="seller">
          <el-select v-model="dataForm.seller" placeholder="请选择" style="width:100%">
            <el-option  label="请选择" value="请选择"></el-option>
            <el-option  label="分销员" value="分销员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="请填写禁用原因"
      width="40%"
      :visible.sync="editDialogVisibleStatus"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules1"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="id" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  prop="replyContent">
          <el-input v-model="dataForm.replyContent" auto-complete="off" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisibleStatus = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitFormStatus"
          :loading="editLoading"
        >{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import CyTable from "@/views/Core/CyTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import {validatePhone} from "@/utils/validate";

  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        labelPosition: 'right',
        size: "small",
        filters: {
          account: "",
          seller: "",
          startDate: "",
          endDate: "",
          status: "",
          startpeople: undefined,
          endpeople: undefined,
          effectivepeople: undefined,
          effectivepeoples: undefined,
          entrybonus: undefined,
          entrybonuss: undefined,
          sellerorder: undefined,
          sellerorders: undefined,
          sales: undefined,
          saless: undefined,
          entrymoney: undefined,
          entrymoneys: undefined,
          t: "seller",
          time:""
        },
        statusOption: [{status: "停用", label: "停用"}, {status: "启用", label: "启用"}],
        columns: [
          {prop: "account", label: "分销员账号", minWidth: 150},
          {prop: "levels", label: "分销员等级", minWidth: 100},
          {prop: "jointime", label: "加入时间", minWidth: 120},
          {prop: "inviting_new_people", label: "邀新人数", minWidth: 100},
          {prop: "effective_new_people", label: "有效邀新人数", minWidth: 120},
          {prop: "entrybonus", label: "已入账邀新奖金", minWidth: 120},
          {prop: "un_entrybonus", label: "未入账邀新奖金", minWidth: 120},
          {prop: "sellerorder", label: "分销订单", minWidth: 80},
          {prop: "sales", label: "销售额", minWidth: 100},
          {prop: "entrymoney", label: "已入账分销佣金", minWidth: 130},
          {prop: "un_entrymoney", label: "未入账分销佣金", minWidth: 100},
          {prop: "statusval", label: "状态", minWidth: 100}
        ],
        filterColumns: [],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},
        detailParam: {},
        totalSize: 3,
        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editDialogVisibleStatus:false,
        editLoading: false,
        dataFormRules: {
          mobile: [{required: true, validator: validatePhone, trigger: 'change'}],
          seller: [{required: true, message: "请选择分销员等级", trigger: "change"}]
        },
        dataFormRules1: {
          replyContent: [{required: true, message: "请填写禁用原因", trigger: "change"}]
        },
        dataForm:{
          seller:"",
          mobile:"",
          replyContent:""
        },
        //选中ID Array
        idArray: []
      };
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        this.$refs.CyTable.findPage(this.filters);
      },
      handleCustom(){
        this.editDialogVisibleStatus = true;
      },
      // 批量删除
      handleDelete: function (data) {
        if (data != null && data.params != null && data.params.length > 0) {
          let ids = data.params.map(item => item.id).toString();

          var params = {};
          params.t = "seller";
          params.ids = ids;
          var this_ = this;
          this.utils.request.deleteUserInfo(params, function (res) {
            if (res.code == "0000") {
              this_.$message({message: "操作成功", type: "success"});
              this_.findPage(null);
            } else {
              this_.$message({message: "操作失败, " + res.msg, type: "error"});
            }
          });
        }
      },
      // 显示新增界面
      handleAdd: function () {
        this.editDialogVisible = true;
        this.operation = true;
        this.detailParam.t = "seller";
        this.dialogVisible = true;
      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.editDialogVisible = true;
        this.operation = false;
        this.$nextTick(function () {
          this.dataForm = Object.assign({}, params.row);
        });
      },
      handleAvatarSuccess(res, file) {
        let fileUrl = URL.createObjectURL(file.raw);
        if (res.code == "0000") {
          this.$message({message: "操作成功", type: "success"});
          this.findPage();
        } else {
          this.$message({message: "导入失败", type: "error"});
        }
      },
      beforeAvatarUpload(file) {
        console.log(file);
        return true;
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm("确认提交吗？", "提示", {}).then(() => {
              let params = Object.assign({}, this.dataForm);

              let this_ = this;
              this_.$refs["dataForm"].resetFields();
              this.$message({message: "操作成功", type: "success"});
              this_.editDialogVisible = false;
            });
          }
        });
      },
      // 编辑
      submitFormStatus: function () {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm("确认提交吗？", "提示", {}).then(() => {
              let params = Object.assign({}, this.dataForm);

              let this_ = this;
              this_.$refs["dataForm"].resetFields();
              this.$message({message: "操作成功", type: "success"});
              this_.editDialogVisibleStatus = false;
            });
          }
        });
      },

      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        var temp = [];

        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "积分商城下载";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {
          });
      }
    },
    mounted() {
      this.initColumns();
    }
  };
</script>

<style scoped>
  .el-icon-plus {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .el-icon-plus .el-upload:hover  {
    border-color: #2F4554;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px !important;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
